package wireless.frequent.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import wireless.frequent.dao.GetFrequentList;
import wireless.frequent.pac.Pac;
import wireless.frequent.pojo.Frequent;

public class Compare {

	private List<List<Pac>> pacList;
	
	public List<List<Pac>> getPacList() {
		return pacList;
	}

	public void setPacList(List<List<Pac>> pacList) {
		this.pacList = pacList;
	}

	public String execute() throws Exception{
		pacList = getPacList("D:\\wireless\\frequent\\package.txt");
		return "success";
	}
	
	private  int isInFrequent(String c){
		GetFrequentList getFreList = new GetFrequentList();
		List<Frequent> freList= new ArrayList<Frequent>();
		freList = getFreList.getFreList();
		 for(Iterator sit=freList.iterator();sit.hasNext();)//迭代取值
	     {
			 Frequent frequent=(Frequent)sit.next();
			 String value = frequent.getValue();
			 value=Long.toHexString(Long.parseLong(value, 2));
			 if(value.startsWith(c)){
				 return 1;
			 }
	     }
		 return 0;
	}
	
	@SuppressWarnings("unused")
	private List<List<Pac>> getPacList(String filePath){
		List<List<Pac>> pacList=new ArrayList<List<Pac>>();
		File file = new File(filePath);
		BufferedReader reader = null;
		 try {
			 reader = new BufferedReader(new FileReader(file));
			 String tempString = null;
			 int line = 0;
			 // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                // 显示行号
	                System.out.println("line " + line + ": " + tempString);
	                List<Pac> list = new ArrayList<Pac>();
	                //获取每一个Pac List
	                for(int i=0;i<tempString.length();i++)
	                {
	                	Pac pac = new Pac();
	                	String value = String.valueOf(tempString.charAt(i));
		                pac.setFlag(isInFrequent(value));
		                pac.setValue(String.valueOf(value));
		                list.add(pac);
	                }
	                
	                pacList.add(list);
	                line++;
	                
	            	}		
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
	        return pacList;
	}
	
	
}
